package service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import pojo.Comment;
import pojo.Customer;
import pojo.Merchant;
import pojo.Order;
import service.MerchantManager;
import dao.MerchantDao;
import dto.MerchantDto;
import util.HttpUtil;
import util.JsonUtil;

@Service(value="merchantManager")
public class MerchantManagerImpl implements MerchantManager {
	@Resource(name="merchantDao")
	private MerchantDao merchantDao;

	@Override
	public List<Merchant> findByType(String typeId) {
		List<Merchant> merchantList = new ArrayList<Merchant>();
		if("all".equals(typeId)){
			merchantList = merchantDao.findAll();
		}
		else{
			merchantList = merchantDao.findByType(typeId);
		}
		
		//这里要处理哪些商家是合法的,并更新其avatar,address,id字段
		List<MerchantDto> PassMerchantList = null;
		try {
			//test httpString
			String httpString = HttpUtil.getResponseStr("http://ZHA-ITA118-w7:8080/spring-admin/api/customer/list");
//			System.out.println(httpString);
			PassMerchantList = (List<MerchantDto>) JsonUtil.toJsonListObject(httpString, List.class, MerchantDto.class);
		} catch (Exception e) {
			System.out.println("a error find in http connect.........................");
			e.printStackTrace();
		}

		List<Merchant> updateMerchantList = new ArrayList<Merchant>();
		for(int i = 0; i < merchantList.size(); i++ ){
			for(int j = 0; j < PassMerchantList.size(); j++){
				if(merchantList.get(i).getName().equals(PassMerchantList.get(j).getName())){
					Merchant addMerchant = new Merchant();
					addMerchant.setId(merchantList.get(i).getId());
					addMerchant.setName(PassMerchantList.get(j).getName());
					addMerchant.setAddress(PassMerchantList.get(j).getAddress());
					addMerchant.setAvatarPath(PassMerchantList.get(j).getAvatar());
					updateMerchantList.add(addMerchant);
				}
			}
		}

		for(Merchant i:updateMerchantList){
			System.out.println(i.getName());
		}
		return updateMerchantList;
	}

	@Override
	public Merchant findById(String id) {
		return merchantDao.findById(id);
	}

	@Override
	@Transactional
	public List<Comment> loadCommentByMerchantId(String id) {
		List<Comment> commentList = new ArrayList<Comment>();
		Merchant merchant = merchantDao.findById(id);
		List<Order> list = merchant.getOrderList();
		
		Comment comment = null;
		for(Order o : list){
			for(Comment c : o.getComment()){
				commentList.add(c);
			}
		}
		return commentList;
	}

	@Override
	@Transactional
	public List<Integer> loadCommentStar(String id) {
		List<Integer> starList = new ArrayList<Integer>();
		Merchant merchant = merchantDao.findById(id);
		
		List<Order> list = merchant.getOrderList();
		
		for(Order o : list){
			if(o.getStar() != null)
				starList.add(new Integer(o.getStar()));
		}
		return starList;
	}
	
	@Override
	public List<Order> getOrderByMerchantId(String id) {
		List<Order> list =  merchantDao.getOrderByMerchantId(id);
		List<Order> voList = new ArrayList<Order>();
		for(Order order :list){
			Order vo = new Order();
			Customer customer = new Customer();
			customer.setName(order.getCustomer().getName());
			vo.setCustomer(customer);
			vo.setProcessNumber(order.getProcessNumber());
			vo.setCreateDate(order.getCreateDate());
			vo.setStar(order.getStar());
			vo.setComment(order.getComment());
			voList.add(vo);
		}
		return voList;
	}
}
